import util from '@/libs/util';
import Cookies from 'js-cookie';

export default {
    name: 'exportable-table',
    data () {
        return {
            columns: [{
                title: '部门名称',
                width: 100,
                key: 'name'
            }, {
                title: '所属机构',
                width: 100,
                key: 'parentname'
            }, {
                title: '已分配专业',
                key: 'majors'
            }, {
                title: '操作',
                key: 'action',
                width: 100,
                align: 'center',
                render: (h, params) => {
                    return h('div', [
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.allotMajor(params.row);
                                }
                            }
                        }, '分配专业')
                    ]);
                }
            }],
            baseuser: JSON.parse(Cookies.get('baseUser')),
            data: [],
            formData: {},
            query: {
                name: '',
                subjectid: ''
            },
            selectYesOrNo: [{id: 'YES', name: '是'}, {id: 'NO', name: '否'}],
            subjectList: [],

            total: 0,
            pageIndex: 1,
            pageSize: 10,

            editModal: false,
            childData: {},
            isRead: {
                parentid: false,
                subjectid: false
            },
            editTitle: '分配专业',
            majorList: [],
            treeModal: false,
            ids: [],
            names: ''
        };
    },
    mounted () {
        this.loadList();
    },
    methods: {
        loadList () {
            util.ajax.post('/busine/organize/major/get', util.toFormData({
                id: this.baseuser.orgId
            })).then(res => {
                this.data = res.data.data;
            });
        },
        allotMajor (obj) {
            this.formData = {};
            for (var key in obj) {
                this.formData[key] = obj[key];
            }
            util.ajax.post('/busine/organize/major/organizeToMajor', util.toFormData({
                organizeid: this.formData.id
            })).then(res => {
                this.ids = [];
                let names = [];
                res.data.data.forEach((item, index) => {
                    this.ids.push(item.majorid);
                    names.push(item.major.name);
                });
                this.names = names.toString();
                this.editModal = true;
            });
        },
        getMajorList () {
            util.ajax.post('/busine/major/findAll', util.toFormData({
                orgid: this.baseuser.orgId
            })).then(res => {
                if (res.data.message === 'SUCCESS') {
                    let allMajor = res.data.data;
                    let majorList = [];
                    allMajor.forEach((item, index) => {
                        if (item.level === 1) {
                            majorList.push({
                                id: item.id,
                                title: item.name,
                                expand: true,
                                children: []
                            });
                        }
                    });
                    allMajor.forEach((item, index) => {
                        if (item.level === 2) {
                            majorList.forEach(item2 => {
                                if (item.parentid === item2.id) {
                                    item2.children.push({
                                        id: item.id,
                                        parentid: item.parentid,
                                        title: item.name,
                                        expand: true,
                                        checked: this.ids.indexOf(item.id) > -1
                                    });
                                }
                            });
                        }
                    });
                    this.majorList = majorList;
                    this.treeModal = true;
                } else {
                    this.$Modal.error({
                        title: '警告',
                        content: '出错了'
                    });
                }
            });
        },
        setMajor () {
            let nodeObj = this.$refs.majorList.getCheckedNodes();
            this.ids = [];
            let names = [];
            nodeObj.forEach(item => {
                if (item.parentid) {
                    this.ids.push(item.id);
                    names.push(item.title);
                }
            });
            this.names = names.toString();
        },
        save () {
            util.ajax.post('/busine/organize/major/saveByOrganize', util.toFormData({
                ids: this.ids,
                organizeid: this.formData.id
            })).then(res => {
                if (res.data.message === 'SUCCESS') {
                    this.$Message.success('保存成功！');
                    this.loadList();
                    this.editModal = false;
                }
            });
        }
    }
}